/**
 * 获取定位信息
 */
const getLocation = data => {
    return new Promise((resolve, reject) =>{
        wx.getLocation({
            type: "wgs84",
            success: res => {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                //var json = { latitude: latitude, longitude: longitude};
                resolve(res.latitude + ',' + res.longitude);
                //resolve(json);
            },
            fail: res => {
                resolve('');
            }
        })
    })
    
}
/**
 * 将定位坐标换成城市ID
 */
const getCityId = location => {
    return new Promise((resolve, reject) =>{
        wx.request({
            url: 'https://weixin.jirengu.com/weather/cityid',
            data: {location},
            success: res => {
                if (res.data && res.data.status == 0 && res.data.data){
                    resolve(res.data.data); 
               }else{
                    reject({status:"error",msg:"获取城市ID失败"});
               }
            },
            fail() {
                reject({ status: "error", msg: "获取城市ID失败" });
            }
        })
    })
}
/**
 * 城市ID获取当前程序天气预报信息
 */
const getNowWeather = cityid =>{
    return new Promise((resolve, reject) => {
        wx.request({
            url: 'http://tj.nineton.cn/Heart/index/all',
            data: { city:cityid },
            success: res => {
                if (res.data && res.data.weather && res.data.weather.length > 0) {
                    resolve(res.data.weather[0]);
                } else {
                    reject({ status: "error", msg: "获取天气失败" });
                }
            },
            fail() {
                reject({ status: "error", msg: "获取天气失败" });
            }
        })
     });
}
const get24hWeather = cityid => {
    return new Promise((resolve, reject) => {
        wx.request({
            url: 'http://tj.nineton.cn/Heart/index/future24h',
            data: { city: cityid },
            success: res => {
                if (res.data && res.data.status == "OK" && res.data.hourly.length > 0) {
                    resolve(res.data.hourly);
                } else {
                    reject({ status: "error", msg: "获取天气失败" });
                }
            },
            fail() {
                reject({ status: "error", msg: "获取天气失败" });
            }
        })
    });
}
module.exports = {
    getLocation,
    getCityId,
    getNowWeather,
    get24hWeather
}